<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="内容" align="center" prop="content"/>
      <el-table-column label="是否已读,是否已答 0否 1是" align="center" prop="readFlag"/>
      <el-table-column label="阅读/问答时间" align="center" prop="readTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.readTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.type === '1' ? '系统通知' : scope.row.type === '2' ? '问卷调查' : scope.row.type === '3' ? '普通订单' : '积分订单' }}</span>
        </template>
      </el-table-column>>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:system:qiushimessage:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <div v-if="form.type === '2'">
          <el-form-item label="表单名" prop="name">
            <span>{{form.name}}</span>
          </el-form-item>
          <el-form-item label="表单描述" prop="description">
            <span>{{form.description}}</span>
          </el-form-item>
        </div>

        <div v-else-if="form.type === '3'">
          <el-form-item label="用户名" prop="userName">
            <span>{{form.userName}}</span>
          </el-form-item>
          <el-form-item label="订单流水" prop="no">
            <span>{{form.no}}</span>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <span>{{form.status === '1' ? '待付款' : form.status === '2' ? '待发货' : form.status === '3' ? '待收货' : form.status === '4' ? '已取消' : '已完成'}}</span>
          </el-form-item>
          <el-form-item label="购买的商品数量" prop="productCount">
            <span>{{form.productCount}}</span>
          </el-form-item>
          <el-form-item label="第三方支付流水号" prop="payTradeNo">
            <span>{{form.payTradeNo}}</span>
          </el-form-item>
          <el-form-item label="订单支付时间" prop="payTime">
            <span>{{ parseTime(form.payTime) }}</span>
          </el-form-item>
          <el-form-item label="支付成功的支付渠道" prop="payChannel">
            <span>{{form.payChannel}}</span>
          </el-form-item>
          <el-form-item label="订单完成时间" prop="finishTime">
            <span>{{ parseTime(form.finishTime) }}</span>
          </el-form-item>
          <el-form-item label="订单取消时间" prop="cancelTime">
            <span>{{ parseTime(form.cancelTime) }}</span>
          </el-form-item>
          <el-form-item label="发货时间" prop="deliveryTime">
            <span>{{ parseTime(form.deliveryTime) }}</span>
          </el-form-item>
          <el-form-item label="收货时间" prop="receiveTime">
            <span>{{ parseTime(form.receiveTime) }}</span>
          </el-form-item>
          <el-form-item label="快递方式" prop="logisticsCode">
            <span>{{form.logisticsCode}}</span>
          </el-form-item>
          <el-form-item label="快递单号" prop="logisticsNo">
            <span>{{form.logisticsNo}}</span>
          </el-form-item>
          <el-form-item label="收件人名称" prop="receiverName">
            <span>{{form.receiverName}}</span>
          </el-form-item>
          <el-form-item label="收件人手机" prop="receiverMobile">
            <span>{{form.receiverMobile}}</span>
          </el-form-item>
          <el-form-item label="收件人详细地址" prop="receiverDetailAddress">
            <span>{{form.receiverDetailAddress}}</span>
          </el-form-item>
        </div>

        <div v-else-if="form.type === '4'">
          <el-form-item label="用户名" prop="userName">
            <span>{{form.userName}}</span>
          </el-form-item>
          <el-form-item label="订单流水" prop="no">
            <span>{{form.no}}</span>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <span>{{form.status === '1' ? '待付款' : form.status === '2' ? '待发货' : form.status === '3' ? '待收货' : form.status === '4' ? '已取消' : '已完成'}}</span>
          </el-form-item>
          <el-form-item label="商品名称" prop="spuName">
            <span>{{form.spuName}}</span>
          </el-form-item>
          <el-form-item label="商品图片" prop="image">
            <template slot-scope="scope">
              <image-preview :src="form.image" :width="50" :height="50"/>
            </template>
          </el-form-item>
          <el-form-item label="第三方支付流水号" prop="payTradeNo">
            <span>{{form.payTradeNo}}</span>
          </el-form-item>
          <el-form-item label="订单支付时间" prop="payTime">
            <span>{{ parseTime(form.payTime) }}</span>
          </el-form-item>
          <el-form-item label="支付成功的支付渠道" prop="payChannel">
            <span>{{form.payChannel}}</span>
          </el-form-item>
          <el-form-item label="订单完成时间" prop="finishTime">
            <span>{{ parseTime(form.finishTime) }}</span>
          </el-form-item>
          <el-form-item label="总价" prop="totalPoint">
            <span>{{form.totalPoint}}</span>
          </el-form-item>
          <el-form-item label="实际付款积分" prop="actualPoint">
            <span>{{form.actualPoint}}</span>
          </el-form-item>
          <el-form-item label="发货时间" prop="deliveryTime">
            <span>{{ parseTime(form.deliveryTime) }}</span>
          </el-form-item>
          <el-form-item label="收货时间" prop="receiveTime">
            <span>{{ parseTime(form.receiveTime) }}</span>
          </el-form-item>
          <el-form-item label="快递方式" prop="logisticsCode">
            <span>{{form.logisticsCode}}</span>
          </el-form-item>
          <el-form-item label="快递单号" prop="logisticsNo">
            <span>{{form.logisticsNo}}</span>
          </el-form-item>
          <el-form-item label="收件人名称" prop="receiverName">
            <span>{{form.receiverName}}</span>
          </el-form-item>
          <el-form-item label="收件人手机" prop="receiverMobile">
            <span>{{form.receiverMobile}}</span>
          </el-form-item>
          <el-form-item label="收件人详细地址" prop="receiverDetailAddress">
            <span>{{form.receiverDetailAddress}}</span>
          </el-form-item>
        </div>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {listMessage, getMessage, delMessage, addMessage, updateMessage} from "@/api/qiushi/system/qiushimessage";
import {getForm} from "@/api/qiushi/followup/qiushiform";
import {getOrder} from "@/api/qiushi/mall/tradeorder";
import {getPointOrder} from "@/api/qiushi/mall/tradepointorder";

export default {
  name: "Qiushimessage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息管理表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        title: null,
        content: null,
        readFlag: null,
        readTime: null,
        type: null,
        joinId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "$comment不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        readFlag: [
          {required: true, message: "是否已读,是否已答 0否 1是不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "类型  1.系统通知  2.问卷调查 3普通订单消息 4积分订单消息不能为空", trigger: "change"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消息管理列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        content: null,
        readFlag: null,
        readTime: null,
        type: null,
        joinId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,

        userName: null,

        name: null,
        description: null,

        no: null,
        status: null,
        productCount: null,
        payTradeNo: null,
        payTime: null,
        finishTime: null,
        cancelTime: null,
        deliveryTime: null,
        receiveTime: null,
        logisticsCode: null,
        logisticsNo: null,
        receiverName: null,
        receiverMobile: null,
        receiverDetailAddress: null,

        spuId: null,
        spuName: null,
        count: null,
        image: null,
        payChannel: null,
        totalPoint: null,
        actualPoint: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      if (row.type === '2') {
        getForm(row.joinId).then(response => {
          this.form = response.data;
          this.form.type = '2';
          this.open = true;
          this.title = "详情";
        });
      } else if (row.type === '3') {
        getOrder(row.joinId).then(response => {
          this.form = response.data;
          this.form.type = '3';
          this.open = true;
          this.title = "详情";
        });
      } else if (row.type === '4') {
        getPointOrder(row.joinId).then(response => {
          this.form = response.data;
          this.form.type = '4';
          this.open = true;
          this.title = "详情";
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息管理编号为"' + ids + '"的数据项？').then(function () {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/system/qiushimessage/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
